package com.example.administrator.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.io.File;
import java.io.FilenameFilter;
import java.net.FileNameMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/3.
 */

public class SetAlarm extends Activity {
    private ListView listV;
    private SimpleAdapter sa;
    private  static final String MUSIC_PATH = new String("/sdcard/");

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.musicmain);
        listV = (ListView) findViewById(R.id.list);
        musicList();
        super.onCreate(savedInstanceState);
    }

    public void musicList() {
        File home = new File(MUSIC_PATH);
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        if (home.listFiles(new MusicFilter()).length > 0) {
            for (File file : home.listFiles(new MusicFilter())) {
                Map<String,String> map = new HashMap<String,String>();
                map.put("musicName",file.getName());
                list.add(map);
            }
            sa = new SimpleAdapter(SetAlarm.this,list,R.layout.musicitems,new String []{"musicName"},new int[]{R.id.musicName});
            listV.setAdapter(sa);
        }
    }
    class MusicFilter implements FilenameFilter{

        @Override
        public boolean accept(File dir, String name) {

            return (name.endsWith(".mp3"));
        }
    }

}
